import { Component, ViewEncapsulation, ChangeDetectionStrategy, ElementRef, ChangeDetectorRef } from '@angular/core';
import { LvConfig } from '../core/config';
import { NotificationContainerService } from './notification.container.service';

@Component({
  selector: 'lv-notification-container',
  exportAs: 'lvNotificationContainer',
  templateUrl: './notification.container.component.html',
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificationContainerComponent {
  constructor(
    public cdr: ChangeDetectorRef,
    public el: ElementRef,
    public notificationContainer: NotificationContainerService,
  ) {}

  remove(id): void {
    this.notificationContainer.remove(this.cdr, id);
  }

  removeAll(): void {
    this.notificationContainer.notifications = [];
    this.cdr.detectChanges();
  }

  create(options): void {
    const isExist = this.notificationContainer.notifications.some((item): boolean => item.id === options.id);
    if (isExist) {
      return;
    }

    if (this.notificationContainer.notifications.length > LvConfig.notificationOptions.lvMaxStacks - 1) {
      this.notificationContainer.notifications.splice(0, 1);
    }
    this.notificationContainer.notifications.push(options);
    this.cdr.markForCheck();
  }
}
